using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UI_InGame : MonoBehaviour
{
    [SerializeField] private PlayerStats playerStats; // 玩家状态
    [SerializeField] private Slider slider; // 血量条

    [SerializeField] private Image dashImage; // 冲刺技能图标
    [SerializeField] private Image parryImage; // 格挡技能图标
    [SerializeField] private Image crystalImage; // 水晶技能图标
    [SerializeField] private Image swordImage; // 剑技能图标
    [SerializeField] private Image blackholeImage; // 黑洞技能图标
    [SerializeField] private Image flaskImage; // 药瓶图标

    [Header("Souls info")]
    [SerializeField] private TextMeshProUGUI currentSouls; // 当前灵魂数量
    [SerializeField] private float soulsAmount;
    [SerializeField] private float increaseRate = 100;


    private SkillManager skills; // 技能管理器

    void Start()
    {
        if (playerStats != null)
        {
            playerStats.onHealthChanged += UpdateHealthUI; // 订阅玩家血量变化事件
        }
        skills = SkillManager.instance; // 获取技能管理器实例
        soulsAmount = (int)(PlayerManager.instance.currency * .85);
    }

    void Update()
    {
        UpdateSoulsUI();

        if (Input.GetKeyDown(KeyCode.LeftShift) && skills.dash.dashUnlocked)
        {
            SetCoolDownOf(dashImage); // 设置冲刺技能冷却
        }
        if (Input.GetKeyDown(KeyCode.K) && skills.parry.parryUnlocked)
        {
            SetCoolDownOf(parryImage); // 设置格挡技能冷却
        }
        if (Input.GetKeyDown(KeyCode.F) && skills.crystal.crystalUnlocked)
        {
            SetCoolDownOf(crystalImage); // 设置水晶技能冷却
        }
        if (Input.GetKeyDown(KeyCode.Mouse1) && skills.sword.swordUnlocked)
        {
            SetCoolDownOf(swordImage); // 设置剑技能冷却
        }
        if (Input.GetKeyDown(KeyCode.R) && skills.blackhole.blackholeUnlocked)
        {
            SetCoolDownOf(blackholeImage); // 设置黑洞技能冷却
        }
        if (Input.GetKeyDown(KeyCode.Q) && Inventory.instance.GetEquipment(EquipmentType.Flask) != null)
        {
            SetCoolDownOf(flaskImage); // 设置药瓶冷却
        }

        CheckCooldownOf(dashImage, skills.dash.cooldown); // 检查冲刺技能冷却
        CheckCooldownOf(parryImage, skills.parry.cooldown); // 检查格挡技能冷却
        CheckCooldownOf(crystalImage, skills.crystal.cooldown); // 检查水晶技能冷却
        CheckCooldownOf(swordImage, skills.sword.cooldown); // 检查剑技能冷却
        CheckCooldownOf(blackholeImage, skills.blackhole.cooldown); // 检查黑洞技能冷却
        CheckCooldownOf(flaskImage, Inventory.instance.flaskCoolDown); // 检查药瓶冷却
    }

    private void UpdateSoulsUI()
    {
        if (soulsAmount < PlayerManager.instance.currency)
        {
            soulsAmount += Time.deltaTime * increaseRate;
        }
        else
        {
            soulsAmount = PlayerManager.instance.GetCurrency();
        }

        // currentSouls.text = PlayerManager.instance.GetCurrency().ToString("#,#"); // 更新当前灵魂数量
        currentSouls.text = ((int)soulsAmount).ToString(); // 更新当前灵魂数量
    }

    // HP的UI更新
    private void UpdateHealthUI()
    {
        slider.maxValue = playerStats.GetMaxHealthValue(); // 设置血量条最大值
        slider.value = playerStats.currentHealth; // 更新血量条当前值
    }

    private void SetCoolDownOf(Image _image)
    {
        if (_image.fillAmount <= 0)
        {
            _image.fillAmount = 1; // 设置图标冷却填充量
        }
    }

    private void CheckCooldownOf(Image _image, float _cooldown)
    {
        if (_image.fillAmount > 0)
        {
            _image.fillAmount -= 1 / _cooldown * Time.deltaTime; // 更新图标冷却填充量
        }
    }
}